var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../@babel/runtime/helpers/toConsumableArray"), n = e(require("../../@babel/runtime/regenerator")), a = require("../../@babel/runtime/helpers/asyncToGenerator"), r = getApp(), i = require("../../utils/api.js"), s = require("../../http/productApi.js");

Page({
    productId: -1,
    boxId: -1,
    shakeLimit: 1,
    shakeLastTime: 0,
    shakeSpace: 2e3,
    opening: !1,
    innerAudioContext: null,
    data: {
        inited: !1,
        systemInfo: r.data.systemInfo,
        shaking: !1,
        modeScrollLeft: 0,
        showCardDialog: !1
    },
    onLoad: function(e) {
        this.productId = e.productId, this.boxId = e.boxId, this.innerAudioContext = wx.createInnerAudioContext(), 
        this.innerAudioContext.src = "/shake.mp3", wx.setInnerAudioOption({
            obeyMuteSwitch: !1
        }).catch(function(e) {});
    },
    onShow: function() {
        var e = this;
        r.data.loginInfo && (this.data.inited || this.reloadDetail()), wx.startAccelerometer(), 
        wx.onAccelerometerChange(function(t) {
            if (!(t.x < e.shakeLimit && t.y < e.shakeLimit && t.z < e.shakeLimit)) {
                var n = new Date().getTime();
                n - e.shakeLastTime < e.shakeSpace || (e.shakeLastTime = n, e.shake());
            }
        });
    },
    onHide: function() {
        wx.offAccelerometerChange(), wx.stopAccelerometer();
    },
    onUnload: function() {
        wx.offAccelerometerChange(), wx.stopAccelerometer();
    },
    changeOnTap: function() {
        var e = this;
        return a(n.default.mark(function t() {
            var a;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return e.setData({
                        inited: !1,
                        shakeTips: null
                    }), wx.showLoading({
                        title: "加载中…"
                    }), t.prev = 2, t.next = 5, s.tryChange(e.productId, e.boxId);

                  case 5:
                    return a = t.sent, e.boxId = a.data.boxId, t.next = 9, s.trySkuList(e.productId, e.boxId);

                  case 9:
                    a = t.sent, e.setData({
                        inited: !0,
                        modelList: a.data
                    }), t.next = 16;
                    break;

                  case 13:
                    t.prev = 13, t.t0 = t.catch(2), console.log(t.t0);

                  case 16:
                    return t.prev = 16, wx.hideLoading(), t.finish(16);

                  case 19:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 2, 13, 16, 19 ] ]);
        }))();
    },
    closeOnTap: function() {
        r.returnUI();
    },
    openOnTap: function() {
        var e = this;
        return a(n.default.mark(function t() {
            var a, r;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (!e.opening && null == e.data.resultInfo) {
                        t.next = 2;
                        break;
                    }
                    return t.abrupt("return");

                  case 2:
                    return e.opening = !0, t.prev = 3, t.next = 6, s.tryOpen(e.productId, e.boxId);

                  case 6:
                    a = t.sent, wx.stopAccelerometer(), r = e.findIndexByid(a.data.openBoxSkuId), e.setData({
                        resultInfo: e.data.modelList[r]
                    }), t.next = 15;
                    break;

                  case 12:
                    t.prev = 12, t.t0 = t.catch(3), console.log(t.t0);

                  case 15:
                    return t.prev = 15, e.opening = !1, t.finish(15);

                  case 18:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 3, 12, 15, 18 ] ]);
        }))();
    },
    reloadDetail: function() {
        var e = this;
        return a(n.default.mark(function t() {
            var a;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return wx.showLoading({
                        title: "加载中…"
                    }), t.prev = 1, t.next = 4, s.trySkuList(e.productId, e.boxId);

                  case 4:
                    a = t.sent, e.setData({
                        inited: !0,
                        title: "试玩中",
                        bigImage: r.data.cacheTryProduct.demoProductBrightBox.brightBoxAttr.boxPileBigImg,
                        modelList: a.data
                    }), t.next = 11;
                    break;

                  case 8:
                    t.prev = 8, t.t0 = t.catch(1), console.log(t.t0);

                  case 11:
                    return t.prev = 11, wx.hideLoading(), t.finish(11);

                  case 14:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 8, 11, 14 ] ]);
        }))();
    },
    shake: function() {
        var e = this;
        return a(n.default.mark(function t() {
            var a, r, o, u;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (!e.data.shaking) {
                        t.next = 2;
                        break;
                    }
                    return t.abrupt("return");

                  case 2:
                    return !1 === wx.getStorageSync("VoiceToggle") || e.innerAudioContext && e.innerAudioContext.play(), 
                    e.setData({
                        shaking: !0,
                        shakeTips: null
                    }), t.prev = 5, a = 1200, r = new Date().getTime(), t.next = 10, s.tryShake(e.productId, e.boxId);

                  case 10:
                    if (o = t.sent, !((u = new Date().getTime() - r) < a)) {
                        t.next = 15;
                        break;
                    }
                    return t.next = 15, i.delay(r, a - u);

                  case 15:
                    o.data ? e.tipResult(o.data) : e.tipText(o.msg), t.next = 21;
                    break;

                  case 18:
                    t.prev = 18, t.t0 = t.catch(5), console.log(t.t0);

                  case 21:
                    return t.prev = 21, e.setData({
                        shaking: !1
                    }), t.finish(21);

                  case 24:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 5, 18, 21, 24 ] ]);
        }))();
    },
    tipResult: function(e) {
        if (e && e.notMeSkuId) {
            var t = this.findIndexByid(e.notMeSkuId);
            t >= 0 && (this.data.modelList[t].filtrTag = 1, this.moveToFirst(t));
        }
        this.tipText("根据小派派的多年经验，\n这个不是" + e.notMeSkuTitle);
    },
    findIndexByid: function(e) {
        for (var t = 0; t < this.data.modelList.length; t++) if (this.data.modelList[t].skuId == e) return t;
        return -1;
    },
    moveToFirst: function(e) {
        var n = t(this.data.modelList), a = n.splice(e, 1);
        n.splice(0, 0, a[0]), this.setData({
            modelList: n,
            modeScrollLeft: 0
        });
    },
    tipText: function(e) {
        var t = this;
        this.setData({
            shakeTips: e
        }), null != this.shakeTipsHideTimer && clearTimeout(this.shakeTipsHideTimer), this.shakeTipsHideTimer = setTimeout(function() {
            t.setData({
                shakeTips: null
            });
        }, 3e3);
    }
});